package com.sky.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.annotation.AutoFill;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author fcy
 * @since 2025-09-08
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        this.save( category);
    }
    /**
     * 分类分页查询
     * @param pageDto
     * @return
     */
    @Override
    public Result<PageResult> queryPage(CategoryPageQueryDTO pageDto) {
        //1.构建分页对象
        Page<Category> page=new Page<>(pageDto.getPage(),pageDto.getPageSize());
        //2.构建查询条件
        Page<Category> res = lambdaQuery()
                .eq(pageDto.getType()!=null,Category::getType, pageDto.getType())
                .like(pageDto.getName()!=null,Category::getName, pageDto.getName())
                .page(page);
        //3.封装结果
        PageResult pageResult = new PageResult();
        pageResult.setTotal(res.getTotal());
        pageResult.setRecords(res.getRecords());
        return Result.success(pageResult);
    }
    /**
     * 删除分类
     * @param id
     */
    @Override
    public void delete(Long id) {
        categoryMapper.deleteById(id);
    }
    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void change(CategoryDTO categoryDTO) {
        Category category= this.getById(categoryDTO.getId());
        BeanUtils.copyProperties(categoryDTO,category);
        this.updateById( category);

    }
    /**
     * 分类状态操作
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, String id) {
        Category category=this.getById(id);
        category.setStatus(status);
        this.updateById(category);

    }
    /**
     * 根据类型查询
     * @param type
     * @return
     */
    @Override
    public List<Category> selectType(Integer type) {
        List<Category> list = lambdaQuery()
                .eq(type != null, Category::getType, type)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime)
                .list();
        return list;

    }
}
